package com.feihong.yw189.utils;

import android.app.Activity;
import android.net.Uri;
import android.text.format.DateFormat;
import android.util.Log;

import com.feihong.yw189.app.App;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * 第三方拍照配置
 * 2018/1/15
 * 张鹏
 */

public class TakePhotoUtils {
    public static final String STR_PATH = "/yw189picture/";

    /**
     * @param takePhoto
     * @return
     */
    public static Uri configCamer(TakePhoto takePhoto) {

        File file = new File(getFileDirectory(), getSelfPhotoPath());
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Uri imageUri = Uri.fromFile(file);
        // 配置
        CompressConfig config;
        LubanOptions option = new LubanOptions.Builder().create();
        config = CompressConfig.ofLuban(option);
        config.enableReserveRaw(false); // 是否保留原图
        takePhoto.onEnableCompress(config, false); // 是否显示压缩进度条

        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        builder.setCorrectImage(true);//纠正照片旋转角度
        takePhoto.setTakePhotoOptions(builder.create());
        return imageUri;
    }

    /**
     * 获取根目录
     * @return
     */
    public static File getFileDirectory() {
        Log.e("项目异常getFileDirectory", "------getFileDirectory------" + App.getApp().getApplication().getExternalCacheDir());
        return App.getApp().getApplication().getExternalCacheDir();
//        return Environment.getExternalStorageDirectory();
    }
    /**
     * 获取自定义路径名称
     *
     * @return
     */
    public static String getSelfPhotoPath() {
        return STR_PATH + System.currentTimeMillis() + ".jpg";
    }

    /**
     * 获取时间格式化路径名称
     *
     * @return
     */
    public static String getDateFormatPhotoPath() {
        return STR_PATH + DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
    }

    /**
     * 获取保存路径
     *
     * @return
     */
    public static String getSavePath() {
        return getFileDirectory() + getSelfPhotoPath();
    }

    /**
     * 删除该路径下的所有图片
     */
    public static void deleteAllPhoto(Activity activity) {
        String path = getFileDirectory() + STR_PATH;
        File dir = new File(path);
        if (dir == null || !dir.exists() || !dir.isDirectory()) {
            return;
        }
        for (File file : dir.listFiles()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                deleteAllPhoto(activity);
            }

//            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//            Uri uri = Uri.fromFile(file);
//            intent.setData(uri);
//            activity.sendBroadcast(intent);
        }
        dir.delete();
    }
}
